<?php

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class SyncGoodsSn extends Command
{
    protected function configure()
    {
        $this->setName('SyncGoodsSn')
            ->setDescription('同步商品');
    }

    protected function execute(Input $input, Output $output)
    {
        $orData = Db::connect('oracle')->table('tm_part')
            ->field(
                '
                NAME as "goods_name",
                TM_PART_ID as "goods_id",
                MACHINE_TYPE as "company",
                NO as "serial_number",
                MACHINE_REMARK as "describe",
                QR_CODE as "barcode"
                ')->where([
                'DELETE_STATUS' => '0',
            ])->where(function ($query) {
                $query->whereOr('NO', 'like', '1A04-%');
                $query->whereOr('NO', 'like', '1A01-%');

            })
//            ->where('TM_PART_ID', 'f871a976dafe4ba5b656b45925d9a471')
            ->select();
        Db::startTrans();
        try {
            foreach ($orData as $oVal) {
                $productInfoId = Db::connect('oracle')
                    ->table('TM_PRODUCT_INFO')
                    ->where('tm_part_id', $oVal['goods_id'])
                    ->value('tm_product_info_id');

                if (!empty($productInfoId)) {
                    $planOrderIdArr = Db::connect('oracle')
                        ->table('TO_PLAN_ORDER')
                        ->field('to_plan_order_id')
                        ->where('tm_product_info_id', $productInfoId)
                        ->select()->toArray();
                    if (!empty($planOrderIdArr)) {
                        $planOrderIdArr = array_column($planOrderIdArr, 'TO_PLAN_ORDER_ID');
                        $productsn = Db::connect('oracle')
                            ->table('TO_ORDER')
                            ->field('productsn as "sn_number"')
                            ->whereIn('to_plan_order_id', $planOrderIdArr)
                            ->select()->toArray();
                        if (!empty($productsn)) {
                            $productsn = array_unique(array_column($productsn, 'sn_number'));
                            foreach ($productsn as $pVal) {
                                if (empty(Db::table('db_product_sn')->where([
                                    'tenant_id' => 10175,
                                    'goods_id' => $oVal['goods_id'],
                                    'sn_number' => $pVal
                                ])->find())) {
                                    $snData = [
                                        'tenant_id' => 10175,
                                        'goods_id' => $oVal['goods_id'],
                                        'sn_number' => $pVal,
                                        'created_at' => time(),
                                        'creator_id' => 10175
                                    ];
                                    Db::table('db_product_sn')->insert($snData);
                                }
                            }
                        }
                    }
                }
            }
            Db::commit();
            $output->writeln('同步MES商品SN，插入数据成功！');
        } catch (\Exception $exception) {
            Db::rollback();
            $output->writeln('同步MES商品SN，插入数据失败！');
        }
    }
}